<?php
/**
 * Project: AIGC
 * Author: moufer <moufer@163.com>
 * Date: 2024/10/14 13:32
 */

namespace module\ai\library\channel\chat;

use module\ai\library\channel\abstract\Chat;
use module\ai\library\OpenAI\Factory;

class ZhipuChat extends Chat
{
    protected string $baseUrl = 'https://open.bigmodel.cn/api/paas/v4';

    protected function chat(): \OpenAI\Resources\Chat|\module\ai\library\OpenAI\Chat
    {
        if (!$this->client) {
            $this->client = (new Factory())->withApiKey($this->apiKey)
                ->withBaseUri($this->baseUrl)
                ->make();
        }
        $chat = $this->client->chat();
        $chat->withFormatData(function ($data) {
            $data['object'] = $data['object'] ?? 'chat.completion';
            $data['model'] = $data['model'] ?? $this->payload['model'] ?? '';
            return $data;
        });

        return $chat;
    }
}